﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_QuanLyHoSoTamNgung_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ShowDetailCertifiedCode.Visible = False
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            btnSave.Visible = HasPermission(Function_Name.CertifiedTamNgung, Session("RoleID"), 1, Audit_Type.Create)
        End If
    End Sub
    Protected Function Save() As Boolean
        Using data As New VSEntities
            Dim p As New VSModel.CertifiedTamNgung
            Try
                p.CertifiedID = hidID.Value
                p.CertifiedCode = hidCertifiedCode.Value
                p.ThoiGianTamNgung = txtThoigiantamngung.Text.Trim()
                p.BatDate = StringToDate(txtBatdate.Text.Trim())
                p.KetThuc = StringToDate(txtKetthuc.Text.Trim())
                p.LyDo = txtLydo.Text.Trim()
                p.StatusId = TinhTrangHoSo.Nhap
                p.NguoiTao = Session("UserName")
                p.NgayTao = Now
                p.HuyenId = Session("HuyenId")
                data.CertifiedTamNgungs.AddObject(p)
                data.SaveChanges()
                Dim strLog As String = "Giay CN:" & txtCertifiedCode.Text.Trim() & ", tên hộ: " & txtBiz_VietNam.Text.Trim()
                Insert_App_Log("Insert  Tạm ngừng kinh doanh:" & strLog & "", Function_Name.CertifiedTamNgung, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Return True
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Return False
            End Try
        End Using
    End Function
    Protected Sub ResetControl()
        txtThoigiantamngung.Text = ""
        txtBatdate.Text = ""
        txtKetthuc.Text = ""
        txtLydo.Text = ""
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Save() Then
            Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/QuanLyHoSoTamNgung/List.aspx';", Me.Page, True)
        Else
            Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
        End If
    End Sub
    Protected Sub btnSaveAndNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveAndNew.Click
        If Save() Then
            Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');", Me.Page, True)
            ResetControl()
        Else
            Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
        End If
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Response.Redirect("Create.aspx")
    End Sub
    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region

    Protected Sub btnCheckCertifiedCode_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCheckCertifiedCode.Click
        Using data As New VSEntities
            Dim _code = txtCertifiedcodechek.Text.Trim() 'bien certifiedcode
            'filter theo curHuyenId And q.ProvinceId = curHuyenId
            Dim _curHuyenId As Integer = Session("HuyenId")
            Dim p = (From q In data.CertifiedHouseholds
                     Where q.CertifiedCode = _code Or q.Ow_CardID = _code And q.ProvinceId = _curHuyenId
                     Order By q.CertifiedUp_Date Descending Select q).FirstOrDefault

            If Not p Is Nothing Then
                ShowDetailCertifiedCode.Visible = True
                'hidden Filter
                DivCheckCertifiedCode.Visible = False

                hidID.Value = IIf(IsNothing(p.CertifiedID) = True, "", p.CertifiedID)
                hidCertifiedCode.Value = IIf(IsNothing(p.CertifiedCode) = True, "", p.CertifiedCode)
                'Tên người đăng ký kinh doanh
                txtBiz_VietNam.Text = IIf(IsNothing(p.Biz_VietName) = True, "", p.Biz_VietName)
                'Mã cấp giấy chứng nhận
                txtCertifiedCode.Text = IIf(IsNothing(p.CertifiedCode) = True, "", p.CertifiedCode)
                'Tên văn phòng cấp giấy chứng nhận
                txtCertifiedDeptName.Text = IIf(IsNothing(p.CertifiedDeptName) = True, "", p.CertifiedDeptName)
                'Ngày cấp giấy chứng nhận
                If Not p.CertifiedDate Is Nothing Then
                    txtCertifiedDate.Text = CType(p.CertifiedDate, Date).ToString("dd/MM/yyyy")
                End If
                'địa điểm kinh doanh
                txtBiz_HeadOffice.Text = IIf(IsNothing(p.Biz_VietName) = True, "", p.Biz_VietName)
                'điên thoại
                txtOw_Tel.Text = IIf(IsNothing(p.Ow_Tel) = True, "", p.Ow_Tel)
                'fax
                txtOw_Fax.Text = IIf(IsNothing(p.Ow_Fax) = True, "", p.Ow_Fax)
                'Email
                txtOw_Email.Text = IIf(IsNothing(p.Ow_Email) = True, "", p.Ow_Email)
                'website
                txtOw_WebSite.Text = IIf(IsNothing(p.Ow_WebSite) = True, "", p.Ow_WebSite)
            End If
        End Using
    End Sub
End Class
